<?php
// 8/15/2009: Immanuel192 upgrade this module
// New features:
/*
   - File ID: work with ajax select file
   - Composer module work with Configure feature
   
*/
class composersmodule {
	function name() { return "Composers Module"; }
	function description() { return "Manage composers"; }
	function author() { return 'immanuel192'; }
	
	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = '') {
		return array(
			'administrate'=>'Administrate',
			'manage'=>'Manage Listings',
			'configure'=>'Configure'
		);
	}
		
	function show($view,$loc = null, $title = '') {
		global $db;
		include(BASE . "subsystems/getid3/getid3.php");
		
		$getid3 = new getID3();
		
    $config = $db->selectObject('composersmodule_config',"location_data='".serialize($loc)."'");
		if (!$config) {
			//do nothing here yes.
      $config->image_collection=0;
      $config->composer_type=0;
      $config->item_per_page=10;        
		}
		$max_item=$config->item_per_page;
    		
		$template = new template('composersmodule',$view,$loc);
		
		if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
		if (!defined('SYS_FILES')) require_once(BASE.'subsystems/files.php');
		
		$musics = array();
		$composers = $db->selectObjects('composers',"composer_type = {$config->composer_type} AND location_data='".serialize($loc)."' ORDER BY rank DESC LIMIT 0,{$max_item}");
		for($i=0; $i<count($composers); $i++) {
			if ($composers[$i]->file_id <= 0) {
				$composers[$i]->picpath = '';
			} else {
				$file = $db->selectObject('file', 'id='.$composers[$i]->file_id);
				$composers[$i]->picpath = $file->directory.'/'.$file->filename;
			}
			// các album
			//			
			$sql="SELECT ".DB_TABLE_PREFIX."_musics.* FROM ".DB_TABLE_PREFIX."_composers  INNER JOIN ".DB_TABLE_PREFIX."_musics  ON ".DB_TABLE_PREFIX."_musics.composer_id = ".DB_TABLE_PREFIX."_composers.id WHERE ".DB_TABLE_PREFIX."_composers.id={$composers[$i]->id} LIMIT 0,5";
			$musics=$db->selectObjectsBySql($sql);
			for($j=0; $j<count($musics); $j++) {
				/*$album=$db->selectObject('listing','id ='.$musics[$j]->album_id);
	$musics[$j]->downloadable=$album->downloadable;
				if ($musics[$j]->high_quality == 0) {
				$musics[$j]->high_quality = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->high_quality);
				$musics[$j]->high_quality = $file->directory.'/'.$file->filename;
				if (file_exists($musics[$j]->high_quality))
				{
				 $file_info = @$getid3->analyze($musics[$j]->high_quality); 
				  $file_info=$file_info['playtime_string'];
				  $musics[$j]->file_length = $file_info;
				 }
				}
				
				if ($musics[$j]->sound_trackmix == 0) {
				$musics[$j]->sound_trackmix = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sound_trackmix);
				$musics[$j]->sound_trackmix = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->sheet_music == 0) {
				$musics[$j]->sheet_music = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sheet_music);
				$musics[$j]->sheet_music = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->demo == 0) {
				$musics[$j]->demo = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->demo);
				$musics[$j]->demo = $file->directory.'/'.$file->filename;
				}
				
				// get composer name
				$composer=$db->selectObject('listing',"list_type = 1 AND id = {$musics[$j]->composer_id}");
				$composer_name='';
				if ($composer)
				{
					$composer_name=$composer->name;
				}
				
				// for tooltip
				$musics[$j]=musicsmodule::make_info($musics[$j], $composer_name, $album->name);
				*/
			}
			$composers[$i]->musics=$musics;
		}
		
		// get all listings here
		$count_post = $db->countObjects('composers',"composer_type={$config->composer_type} AND location_data='".serialize($loc)."'");
		
		// page split
		// cal how many posts
			$page_count=floor($count_post / $max_item);
			if ($count_post % $max_item != 0)
			  $page_count++;
			// build page array
			$pages = array();
			for ($i=1;$i<=$page_count;$i++)
				$pages[$i-1] = $i;		
		$template->register_permissions(array('administrate','configure'),$loc);
		$template->assign('composers', $composers);
		$template->assign('moduletitle', $title);
		$template->assign('page_count',$page_count);
		$template->assign('pages',$pages);
		$template->assign('musics',$musics);
		$template->output();
	}
	
	function deleteIn($loc) {
		// IMPLEMENTME:deleteIn for the listing module
	}
	
	function copyContent($oloc,$nloc) {
		// IMPLEMENTME:copyContent for the listing module
	}

	function searchName_() {
		return "Composers module";
	}
	
	/*
	   8/26/2009 immanuel192
	   This function create HTML content of spider item
	*/
	function spiderTemplate_($composer)
	{
	   global $db;
	   	if ($composer->file_id <= 0) {
				$composer->picpath = '';
			} else {
				$file = $db->selectObject('file', 'id='.$composer->file_id);
				$composer->picpath = $file->directory.'/'.$file->filename;
			}
			
			// create template content
			$template=new template("composersmodule","_search");
			$template->assign("composer",$composer);
			ob_start();
			$template->output();
			$get_value=ob_get_contents();
			ob_end_clean(); 
			return $get_value;
  }
	
	function spiderContent_($item = null) {
		global $db;
		
		if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php');
		
		$search = null;
		$search->category = 'Composers';
		$search->ref_module = 'composersmodule';
		$search->ref_type = 'composer';
		
		if ($item) {
			$db->delete('search',"ref_module='composersmodule' AND ref_type='composer' AND original_id=" . $item->id);
			$search->original_id = $item->id;
			$search->title = ' ' . $item->name . ' ';
			$search->view_link = 'index.php?module=composersmodule&action=view&id='.$item->id;
			$search->body = composersmodule::spiderTemplate($item); 
			$search->dont_trim=1;
			$search->location_data = $item->location_data;
			$db->insertObject($search,'search');
		} else {
			$db->delete('search',"ref_module='composersmodule' AND ref_type='composer'");
			foreach ($db->selectObjects('composers','') as $item) {
				$search->original_id = $item->id;
				$search->title = ' ' . $item->name . ' ';
				$search->view_link = 'index.php?module=composersmodule&action=view&id='.$item->id;
				$search->body = composersmodule::spiderTemplate($item);
				$search->location_data = $item->location_data;
				$search->dont_trim=1;
				$db->insertObject($search,'search');
			}
		}
		
		return true;
	}
}

?>
